﻿<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>摇号管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="/plugins/layuiadmin/style/admin.css" media="all">
</head>
<body>
  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-card-body">
        <div style="padding-bottom: 10px;">
          <button class="layui-btn layui-btn-normal layuiadmin-btn-list" data-type="add"><i class="layui-icon layui-icon-add-1"></i>新建</button>
          <button class="layui-btn layuiadmin-btn-list" data-type="refresh"><i class="layui-icon layui-icon-refresh"></i>刷新</button>
          <div class="layui-form" lay-filter="dds-search-form" id="dds-search-form" style="float:right">
            <div class="layui-inline">
            <input type="text" class="layui-input" id="starttime" name="starttime" style="width:150px;" autocomplete="off" readonly="readonly" placeholder="开始时间">
            </div>
            <div class="layui-inline">
            <input type="text" class="layui-input" id="endtime" name="endtime" style="width:150px;" autocomplete="off" readonly="readonly" placeholder="结束时间">
            </div>
            <div class="layui-input-inline">
            <select name="typeid" id="typeid">
                <option value="">选择项目类型</option>
            </select>
            </div>
            <div class="layui-inline">
             <input class="layui-input" name="keyword" id="keyword" placeholder="请输入搜索条件">
            </div>
            <button class="layui-btn" lay-submit lay-filter="btn_search"><i class="layui-icon layui-icon-search"></i>搜索</button>
          </div>
        </div>
        <table id="dds-list" lay-filter="dds-list"></table>
        <script type="text/html" id="tpl_lunci">
            第{{d.lun}}轮,第{{d.ci}}次
        </script>
        <script type="text/html" id="tpl_status">
            {{#  if(d.status==1){ }}
            <button class="layui-btn layui-btn-xs">开启</button>
            {{#  } else if(d.status==2) { }}
            <button class="layui-btn layui-btn-normal layui-btn-xs">已摇号</button>
            {{#  } else { }}
            <button class="layui-btn layui-btn-primary layui-btn-xs">关闭</button>
            {{#  } }}
        </script>
        <script type="text/html" id="table-content-list">
          {{#  if(d.status<=1){ }}
          <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
          <a class="layui-btn layui-btn-xs layui-btn-container" lay-event="lottery"><i class="layui-icon layui-icon-edit"></i>摇号</a>
          <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
          {{#  } else if(d.status==2) { }}
          <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="view"><i class="layui-icon layui-icon-edit"></i>查看</a>
          <a class="layui-btn layui-btn-xs layui-btn-container" href="lottery_export?id={{d.id}}" ><i class="layui-icon layui-icon-edit"></i>导出</a>
          <a class="layui-btn layui-btn-xs layui-btn-disabled" lay-event="del2"><i class="layui-icon layui-icon-delete"></i>删除</a>
          {{#  } }}
        </script>
      </div>
    </div>
  </div>
  <script src="/plugins/layui/layui.js"></script> 
  <script>
  layui.config({
    base: '/plugins/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index','table','laydate','form'], function(){
    var $ = layui.$, table = layui.table, form = layui.form, laydate = layui.laydate;

    laydate.render({
      elem: '#starttime' //指定元素
    });

    laydate.render({
      elem: '#endtime' //指定元素
    });

    $.ajax({
        type: "get",
        url: "get_typelist",
        dataType: "json",
        async:false,
        success: function (res) {
            $.each(res.typelist, function (i, v) {
                $("#typeid").append("<option value='" + v.id + "'>" + v.title + "</option>");
            });
            layui.form.render("select");
        }
    });

    table.render({
      elem: '#dds-list'
      , url: 'lottery_list' //接口
      , cols: [[
          { field: 'name', title: '项目名称', minWidth: 100, align: 'center' }
        , { field: 'typename', title: '项目类型', minWidth:100 , align: 'center' }
        , { field: 'maxnum', title: '单位总数', width:90 , align: 'center' }
        , { field: 'lotnum', title: '中标数量', width: 90 , align: 'center'}
        , { field: 'luckynum', title: '中标号码', width: 120 }
        , { field: 'lun', title: '轮次',width:120,align: 'center', templet: '#tpl_lunci' }
        , { field: 'ltdate', title: '添加时间', width: 160, align: 'center' }
        , { field: 'status', title: '状态',width:80,align: 'center', templet: '#tpl_status' }
        , { title: '操作', width: 230, align: 'center', fixed: 'right', toolbar: '#table-content-list' }
      ]]
      , page: true
      , request: {
        pageName: 'pageindex', //页码的参数名称，默认：page
        limitName: 'pagesize' //每页数据量的参数名，默认：limit
      }
      , limit: 20
      , limits: [20, 50, 100]
      , text: {
        none: '暂无相关数据'
      }
    });

    //监听搜索
    form.on('submit(btn_search)', function(data){
      var field = data.field;
      //执行重载
      table.reload('dds-list', {
        where: field
      });
    });

    //监听操作
    var active = {
      del: function(){
        var checkStatus = table.checkStatus('dds-list')
        ,checkData = checkStatus.data; //得到选中的数据
        if(checkData.length === 0){
          return layer.msg('请选择数据');
        }

        var ids = [];//存储id的数组
        checkData.forEach(function (item) {
          ids.push(item.id);
        });
      
        layer.confirm('您确定要删除吗？删除后将无法进行恢复。',{icon: 3, title:'提示'}, function (index) {
          $.ajax({
                type: "post",
                url: "lottery_list",
                data: {ids:ids.join(","),action:'delete'},
                dataType: "json",
                success: function (data) {
                    if (data.code == 0)
                    {
                      table.reload('dds-list');
                      layer.msg(data.msg);
                    }
                }
          });
        });
      },
      add: function(){
        layer.open({
          type: 2
          ,title: '新建项目'
          ,content: 'lottery_add.html'
          ,maxmin: true
          ,area: ['700px', '600px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            //点击确认触发 iframe 内容中的按钮提交
            var submit = layero.find('iframe').contents().find("#btn_submit");
            submit.click();
          }
        }); 
      },
      refresh: function () {
        table.reload('dds-list');
      }
    }; 

    $('.layui-btn.layuiadmin-btn-list').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });

    //监听工具条
    table.on('tool(dds-list)', function(obj){
      var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('您确定要删除吗？删除后将无法进行恢复。', { icon: 3, title: '提示' }, function (index) {
                $.ajax({
                    type: "post",
                    url: "lottery_list",
                    data: { ids: data.id,action:'delete' },
                    dataType: "json",
                    success: function (data) {
                        if (data.code == 0) {
                            table.reload('dds-list');
                            layer.msg(data.msg);
                        }
                    }
                });
            });
        }else if(obj.event === 'edit'){
            layer.open({
              type: 2
              ,title: '编辑项目'
              ,content: 'lottery_add.html#/id='+data.id
              ,maxmin: true
              ,area: ['700px', '600px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                var submit = layero.find('iframe').contents().find("#btn_submit");
                submit.trigger('click');
                }
            });
        } else if(obj.event === 'view'){
            layer.open({
              type: 2
              ,title: '查看中标单位'
              ,content: 'lottery_view.html#/id='+data.id
              ,maxmin: true
              ,area: ['950px', '600px']
              ,btn: ['关闭']
            });
        } else if(obj.event === 'export'){
            $.ajax({
                type: "post",
                url: "lottery_list",
                data: {ids:data.id,action:'export'},
                dataType: "json",
                success: function (data) {
                    if (data.code >0)
                    {
                      layer.msg(data.msg);
                    }
                }
          });
        } 
        else if(obj.event === 'lottery'){
            window.open('../lottery.html?id='+data.id);
        } 
    });

  });
  </script>
</body>
</html>
